package com.ittest.service.impl;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.ittest.domain.SMSCode;
import com.ittest.service.SMSCodeService;
import com.ittest.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
public class SMSCodeServiceImpl implements SMSCodeService {

    @Autowired
    private CodeUtils codeUtils;

    //@CreateCache(area = "sms",name = "jetCache",expire = 10,timeUnit = TimeUnit.SECONDS) //这是remote，没有指定使用本地还是远程时，默认是远程
    //@CreateCache(name = "jetCache",expire = 1000,timeUnit = TimeUnit.SECONDS,cacheType = CacheType.LOCAL) //指定了cachetype是local
    @CreateCache(name = "jetCache",expire = 1000,timeUnit = TimeUnit.SECONDS)
    private Cache<String,String> jetcache;


    @Override
    public String sendCodeToSMS(String tele) {
        String code = codeUtils.generator(tele);
        jetcache.put(tele,code);
        return code;
    }

    @Override
    public boolean checkcode(SMSCode smsCode) {
        String code = jetcache.get(smsCode.getTele());
        return code.equals(smsCode.getCode());
    }











}
